package com.recursiveflow.shared.view.image
{
    import flash.display.Loader;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    
    import com.gskinner.motion.GTween;
    import com.recursiveflow.shared.view.display.ChildSprite;
    import com.recursiveflow.shared.view.preloader.Spinner;
    import com.recursiveflow.shared.view.shapes.Background;
    import com.recursiveflow.shared.view.shapes.GradientBackground;
    import com.recursiveflow.shared.view.shapes.Lozenge;    

    public class ExternalImage extends Sprite
    {
        protected static const REVEAL_DURATION : Number = 1;
        
        protected var imageData : Loader;
        protected var sourceUrl : URLRequest;
        protected var showImage : GTween;
        protected var revealTween : GTween;
        protected var background : Background;
        protected var maskShape : Shape;
        protected var spinner : Spinner;        protected var imageHolder : Sprite;
        public function ExternalImage(w : Number, h : Number, bgA : Number = 0xFFFFFF, bgB : Number = 0xFEE1A3) 
        {	
            this.imageHolder = new ChildSprite(this);
            this.maskShape = new Lozenge(this, w, h);
            this.spinner = new Spinner(this);
            
            setupImageHolder(w, h, bgA, bgB);
            setupTweening();
        }
                private function setupImageHolder(w:Number, h:Number, bgA : Number, bgB : Number) : void        {
            background = new GradientBackground(imageHolder, w, h, bgA, bgB);		
			
            imageData = new Loader();
			
            imageData.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
            imageData.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
            imageData.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
			
            imageHolder.addChild(imageData);                        
            imageHolder.mask = maskShape;
        }        
        public function show() : void
        {
        	revealTween.play();
        }  
        
        public function load(url : URLRequest) : void 
        {
            sourceUrl = url;
			
            showImage.position = 0;
            spinner.play();
            imageData.load(url);
        }
        protected function setupTweening() : void
        {
            GTween.timingMode = GTween.TIME;
			
            revealTween = new GTween(this, REVEAL_DURATION);
            showImage = new GTween(imageData, REVEAL_DURATION);

            revealTween.autoPlay = showImage.autoPlay = false;
			
            revealTween.target["alpha"] = 0;
			showImage.target["alpha"] = 0;
			
			revealTween.proxy["alpha"] = 1;
			showImage.proxy["alpha"] = 1;            
        }

        protected function onLoadComplete(event : Event) : void 
        {
            spinner.stop();
            imageData.width = background.width;
            imageData.height = background.height;
            showImage.play();
        }

        protected function onLoadError(e : Event) : void 
        {
            spinner.stop();
            showImage.play();
        }

        protected function onLoadProgress(e : ProgressEvent) : void 
        {
        }
    }
}